package leader

import (
	"gitverse.ru/synapse/kubelatte/pkg/operator"
	"gitverse.ru/synapse/kubelatte/pkg/operator/controllers"
	initManager "gitverse.ru/synapse/kubelatte/pkg/operator/init_manager"
	"net/http"
	"testing"
)

func Test_readyz(t *testing.T) {
	t.Run("not nil syncController and allowed", func(t *testing.T) {
		err := readyz(&http.Request{})
		if err != nil {
			t.Fatal("Err expected to be nil")
		}
	})
}

func Test_healthz(t *testing.T) {
	var wantErr bool
	initMgr = initManager.GetInitManager()
	operator.KbltMutator = "false"

	t.Run("No_resources_in_cluster", func(t *testing.T) {
		wantErr = false

		if err := healthz(nil); (err != nil) != wantErr {
			t.Errorf("healthz() error = %v, wantErr %v", err, wantErr)
		}
	})

	t.Run("Each_controller_is_not_initialized", func(t *testing.T) {
		wantErr = false

		if err := healthz(nil); (err != nil) != wantErr {
			t.Errorf("healthz() error = %v, wantErr %v", err, wantErr)
		}
	})

	t.Run("Each_controller_is_not_started", func(t *testing.T) {
		wantErr = true
		operator.KbltMutator = "true"
		defer func() { operator.KbltMutator = "false" }()

		triggerR = &controllers.TriggerReconciler{}
		templateR = &controllers.TemplateReconciler{}
		scopeR = &controllers.ScopeReconciler{}
		if err := healthz(nil); (err != nil) != wantErr {
			t.Errorf("healthz() error = %v, wantErr %v", err, wantErr)
		}
	})
}
